<?php

namespace haibao\cms\business\activity;

use haibao\cms\data\activity\Activity as ActivityData; 
use haibao\cms\business\BusinessException;

class Activity extends \haibao\cms\business\Base{

    public static function getActivityByFilter($filter,$isGetOne = TRUE){
        $activityData = new ActivityData();
        if($isGetOne){
            return $activityData->getOne($filter);
        }else{
            return $activityData->getAll($filter);
        }
    }
    
    public static function getActivityListById($activityId){
        $activityData = new ActivityData();
        return $result = $activityData->getOneById($activityId);
    }
    
    public static function addActivity($model){
        self::checkOperation(self::FUNC_CODE_ACTIVITY_ADD);
        self::checkActivityModelData($model);
        $activityData = new ActivityData();
        $activityData->addActivityData($model);
        self::delActivityCache($model->Id);
        self::saveOperateHistory(self::FUNC_CODE_ACTIVITY_ADD, $model->toArray(true,true));
    }
    
    public static function editActivity($model){
        self::checkOperation(self::FUNC_CODE_ACTIVITY_EDIT);
        self::checkActivityModelData($model);
        $activityData = new ActivityData();
        $activityData->updateActivityData($model);
        self::delActivityCache($model->Id);
        self::saveOperateHistory(self::FUNC_CODE_ACTIVITY_EDIT, $model->toArray(true,true));
    }
    
    private static function checkActivityModelData($model){
        if(!$model){
            throw new BusinessException('error');
        }
    }
    
    public static function delActivityById($id, $arrSet){
        self::checkOperation(self::FUNC_CODE_ACTIVITY_DEL);
        if (!$id){
            throw new BusinessException('ID不能为空');
        }
        $activityData = new ActivityData();
        $activityData->updateById($id, $arrSet);
        self::delActivityCache($id);
        self::saveOperateHistory(self::FUNC_CODE_ACTIVITY_DEL, array($id,$arrSet));
    }
    
    /**
     * 更新活动表里面的奖品数量
     * @param  $id 活动ID
     * @param string $operateType 其中，1为-num;2为+num;3为-或+
     */
    public static function updateActivityPrizeNumById($id,$num,$operateType = 0,$restNum = 0){
        if(!$id){
            throw new BusinessException('Id不可以为空！');
        }
        $activityData = new ActivityData();
        $activityData->updateActivityPrizeNumById($id,$num,$operateType,$restNum);
        self::delActivityCache($id);
    } 
    
    public static function delActivityCache($activityId){
        $cache = new \haibao\cms\cache\Activity();
        $cache->delActivityCache();
        $cache->delActivityInfoCache($activityId,true);
        $cache->delActivityInfoCache($activityId,false);
    }
    
}